package com.tros.customer.features;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class OtherTransfer
 */
@WebServlet("/OtherTransfer")
public class OtherTransfer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OtherTransfer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Connection connect =null;			 
		HttpSession ses = request.getSession();
		
		String accType = request.getParameter("accType");
		String accNo = request.getParameter("accno");
		String balance = request.getParameter("balAmt");
		String transAmt = request.getParameter("tranAmt");
		String transName = request.getParameter("tranAmt");
		String tranaccNo = request.getParameter("tranaccNo");
		String tranacctype = request.getParameter("rety");
		
		System.out.println("accType :"+accType);
		System.out.println("balance :"+balance);
		System.out.println("transAmt :"+transAmt);
		System.out.println("transName :"+transName);
		System.out.println("tranaccNo :"+tranaccNo);
		System.out.println("accNo :"+accNo);
		
		try{
			  System.out.println("--------------> balance -------> "+Long.parseLong(balance));
			  System.out.println("--------------> transAmt -------> "+Long.parseLong(transAmt));
			  
	           if(Double.parseDouble(balance) < Double.parseDouble(transAmt))
	           {
	        	   ses.setAttribute("transferResult", "Transfer Amount Entered Exceeds Account Balance");
			       response.sendRedirect("passTransfer.jsp");
	           }else
	           {
	        	   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			       connect =DriverManager.getConnection("jdbc:odbc:bank","root", " ");
		           System.out.println("DB Connection created :"+connect);
		           
		           Double bal = Double.parseDouble(balance)-Double.parseDouble(transAmt);
		           System.out.println("----------> amount ---------->"+bal);
		          
		        	   UpdateReq(connect,accType,accNo,transAmt,transName,tranaccNo,ses);
		        	   
		        	   ses.setAttribute("transferResult", "Amount Transfer Request sent successfully for Manager Approval");
				       response.sendRedirect("passTransfer.jsp");
	           }
	           
		}catch(Exception e)
		{
			ses.setAttribute("transferResult", "Amount Transfer Failure \n Please Try again");
	        response.sendRedirect("passTransfer.jsp");
			System.out.println("Failed here --------->"+e.getMessage());
	       	e.printStackTrace();
		}
	}

	private void UpdateReq(Connection connect, String accType, String accNo,
			String transAmt, String transName, String tranaccNo, HttpSession ses) {
		// TODO Auto-generated method stub
		try
		{
			PreparedStatement state1=connect.prepareStatement("INSERT INTO TRANSMNYREQ VALUES(?,?,?,?,?,?,?)");
		    
		    state1.setString(1,String.valueOf(ses.getAttribute("currentID")));
		    state1.setString(2,accNo);
		    state1.setString(3,accType);
		    state1.setString(4,transName);
		    state1.setString(5,tranaccNo);
		    state1.setString(6,transAmt);
		    state1.setString(7,new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date()));
		    
		        state1.executeUpdate();
		        state1.close();
			System.out.println("---------> connect value ------"+connect);
			connect.close();
		}catch(Exception e)
		{
			e.getMessage();
		}
		
		
	}

}
